-- area_damage
-- create by panyl
-- 技能: 范围伤害。以目标为中心，对周围n * n范围敌人造成伤害=魔力 * 威力 * 加成
-- 目前支持如下两种配置：
-- 1、damage(1200,1)：对半径1范围内目标造成1200威力的魔法伤害
-- 2、damage(1200,1,style,2000)：对半径1范围内目标造成1200威力的魔法伤害，如果目标是style系，则效果增强200%


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力，半径
        local base = para[1];
        local radius = para[2];
        local addon_rate = 0;

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害/计算时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 4. 遍历目标周围所有已开且存活的怪
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local c = DungeonM.convertToCoor(p);

            -- 有效范围内
            local near = math.abs(c.x - coor.x) <= radius and math.abs(c.y - coor.y) <= radius;

            local grid = DungeonM.getGridByPos(p);

            -- 翻开且存活的怪物
            if  near and grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        for _, monster in pairs(ret) do
            local finalDamage = damage;
            if #para == 4 and tonumber(para[3]) == monster.style then
                -- 该系别有伤害加成,需重新计算伤害
                finalDamage = math.modf((force + base * para[4] / 1000) * (magic + 3) / 1000);
            end

            -- 魔法暴击类，多种暴击线性叠加伤害
            local addon_damage = 0;

            -- 伤害类魔法概率增强
            local props = fetchProps(source, "damage_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
            end
            -- 魔法暴击
            props = fetchProps(source, "critical_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
            end
            -- 概率双倍
            props = fetchProps(source, "skill_double");
            for _, prop in ipairs(props) do
                if tonumber(prop[2]) == skillId then
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
            end
            -- 最终伤害
            finalDamage = finalDamage + addon_damage;

            -- 魔法抗性
            prop = combine(monster, "resist", "magic");
            -- 忽视抗性
            local p = combine(source, "ignore_resist", skillId);
            prop[3] = PropM_apply(p, prop[3]);
            p = combine(source, "ignore_resist", "magic");
            prop[3] = PropM_apply(p, prop[3]);

            finalDamage = PropM_apply(prop, finalDamage);

            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(monster):hit(source, monster, skillId);
            -- 受创
            CombatM.receiveDamage(source, monster, finalDamage, skillId, extra);
        end

        -- 返回作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");

        if #arr == 2 then
            -- 规则1：area_damage(威力,半径)
            return { tonumber(arr[1]), tonumber(arr[2]) };
        else
            -- 规则2：area_damage(威力,半径,style,2000)
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];
        local addon_rate = 0;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{area_damage}", damageForce);
        else
            desc = string.gsub(desc, "{area_damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        -- 系别TODO

        return desc;
    end,
}
